<template>
  <div class="user-data">
    <div class="title">修改密码</div>
    <el-form :model="form" ref="form" size="medium">
      <el-form-item>
        <div class="item">
          <div class="before">+86</div>
          <div class="input">
            <input
              v-model="form.phone"
              placeholder="请输入手机号码"
              type="text"
            />
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <div class="item">
          <div class="input">
            <input
              v-model="form.verification"
              placeholder="输入验证码"
              type="text"
            />
          </div>
          <div class="after" v-if="!codeTime" @click="sendCode">发送验证码</div>
          <div class="after" v-else>{{ codeTime }}s后重试</div>
        </div>
      </el-form-item>
      <el-form-item>
        <div class="item">
          <div class="input">
            <input
              v-model="form.password"
              placeholder="请输入密码"
              type="password"
            />
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <div class="item">
          <div class="input">
            <input
              v-model="form.password2"
              placeholder="确认密码"
              type="password"
            />
          </div>
        </div>
      </el-form-item>
    </el-form>
    <el-button
      @click="editHander"
      style="width: 160px; margin-top: 20px"
      type="primary"
      >保存</el-button
    >
  </div>
</template>

<script>
import verify from "@/libs/util.verify";
import api from "@/api";

export default {
  name: "UserData",
  data() {
    return {
      form: {
        phone: "",
        password: "",
        password2: "",
        verification: "",
      },
      codeTime: 0, // 验证码发送计时
    };
  },
  created() {},
  methods: {
    // 发送验证码
    sendCode() {
      if (!verify.isPhone(this.form.phone))
        return this.$message.error("不是合法的手机号码");
      // 发送验证码
      api
        .GET_CODE({ phone: this.form.phone })
        .then(() => {
          // 验证码发送成功 开始倒计时
          this.codeTime = 60;
          let timer = setInterval(() => {
            this.codeTime--;
            if (this.codeTime <= 0) {
              this.codeTime = 0;
              clearInterval(timer);
            }
          }, 1000);
        })
        .catch((err) => {
          // 验证码发送失败
          console.error(err);
        });
    },
    editHander() {
      if (!verify.isPhone(this.form.phone)) {
        return this.$message.error("不是合法的手机号码");
      } else if (verify.isEmpty(this.form.password)) {
        return this.$message.error("密码不能为空");
      } else if (this.form.password !== this.form.password2) {
        return this.$message.error("两次密码不一致");
      } else if (verify.isEmpty(this.form.verification)) {
        return this.$message.error("短信验证码不能为空");
      }

      api
        .EDIT_PWD(this.form)
        .then(() => {
          this.$message.success("修改成功，请重新登录");
          this.$store.commit("user/remove");
        })
        .catch((err) => {
          console.error(err);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.user-data {
  border: 1px solid #dedede;
  padding: 20px;
  .title {
    margin-bottom: 28px;
    font-size: 20px;
    font-weight: 600;
    color: #333333;
    line-height: 28px;
  }
  .item {
    display: flex;
    width: 317px;
    height: 40px;
    border-bottom: 1px solid #dedede;
    align-items: center;
    line-height: 20px;
    // margin-bottom: 10px;
    .before {
      cursor: pointer;
      height: 20px;
      padding: 0 10px;
      border-right: 1px solid #f0f0f0;
      font-size: 14px;
      color: #333333;
    }
    .after {
      cursor: pointer;
      height: 20px;
      padding: 0 10px;
      border-left: 1px solid #f0f0f0;
      font-size: 14px;
      color: #333333;
    }
    .input {
      flex: 1;
      input {
        padding: 10px;
        border: none;
      }
    }
  }
}
</style>
